#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
//使用宏
#define ERR_EXIT(m)         \
    do                      \
    {                       \
        perror(m);          \
        exit(EXIT_FAILURE); \
    } while (0)

int main()
{
    int pipefd[2];
    if (pipe(pipefd) == -1)
        ERR_EXIT("pipe error");

    pid_t pid;
    pid = fork();

    if (pid == -1)
        ERR_EXIT("fork error");
    // 子进程关闭读端，父进程关闭写端
    if (pid == 0)
    {
        close(pipefd[0]);
        //子端写入
        write(pipefd[1], "hello", 5);
        close(pipefd[1]);
        exit(EXIT_SUCCESS);
    }

    close(pipefd[1]);
    char buf[10] = {0};
    //父端读取
    read(pipefd[0], buf, 10);
    printf("buf=%s\n", buf);

    return 0;
}